package com.youzikj.wx.core.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;


public class FileUtil {
	
	private static final Logger log = Logger.getLogger(FileUtil.class);
	
	/**
	 * springMVC上传文件
	 * 
	 * @param file
	 * @param desFile
	 * @param modelSuffix　文件后缀名
	 * @return
	 */
	public static String uploadTransfer(MultipartFile file, File desFile,String modelSuffix) {

		// 除掉空格
		if (desFile.getAbsolutePath().indexOf("%20") > 0) {
			desFile = new File(desFile.getAbsolutePath().replaceAll("%20", " "));
		}

		// 日志加载消息
		log.info("上传文件名［:" + file.getOriginalFilename() + "］,"+ "文件大小:［" + file.getSize()/1024+"KB］");
		log.info("上传的指定路径:［" + desFile.getAbsolutePath()+"］");

		// 判断文件是否为空
		if (!hasValue(file.getOriginalFilename())) {
			return "文件不可以为空!";
		}

		// 判断前缀名和后缀名是否满足要求
		int pos = desFile.getAbsolutePath().lastIndexOf(".");
		String prefixPath = desFile.getAbsolutePath().substring(0, pos);
		String suffix = desFile.getAbsolutePath().substring(pos);

//		if (modelSuffix != null) {
//			if (!((modelSuffix.equalsIgnoreCase(suffix) || ("." + modelSuffix)
//					.equalsIgnoreCase(suffix)))) {
//				return "后缀名不对,文件上传失败!(后缀只能是.jpg)";
//			}
//		}

		if (file.getSize() > 9000000) {
			return "文件太大";
		}
		// 判断结束 实现移动
		try {

			// 文件移动到目标路径
			file.transferTo(desFile);
		} catch (Exception e) {
			log.error(e, e);
			return "上传失败";
		}
		return "上传成功";
	}
	
	/**
	 * 获取二维码图片
	 * @throws UnsupportedEncodingException 
	 */
	public static String getQrcode(HttpServletRequest request,String taskId) throws UnsupportedEncodingException{
		String filePath = "";
		String url = null;
		//授权地址
		String s = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxca78deeb014de180&redirect_uri=https://wycd.youzikj.com/getcode.html&response_type=code&state=STATE&scope=snsapi_userinfo#wechat_redirect".replace("STATE", taskId);
		System.out.println("授权地址:"+s);
		url = "http://pan.baidu.com/share/qrcode?w=150&h=150&url="+URLEncoder.encode(s, "utf-8");
				//把地址转换成二维码
				try {
					URL u = new URL(url);
					URLConnection conn = u.openConnection();
					conn.connect();
					String path = request.getSession().getServletContext().getRealPath(File.separator);
					path += File.separator + "upload" + File.separator;
					//将ticket作为文件名
					filePath = path +taskId +".jpg";
					//将危险服务器返回的输入流写入文件
					InputStream in = conn.getInputStream();
					FileOutputStream fos = new FileOutputStream(new File(filePath));
					int i = 0;
					while((i = in.read()) != -1) {
						fos.write(i);
					}
					fos.close();
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		return filePath;
		
	}

	/**
	 * 判断字符串是否为null或空字符串
	 * @param str
	 * @return
	 */
	public static boolean hasValue(String ...str) {
		for (String string : str) {
			if(string==null||"".equals(string)) return false;
		}
		return true;
	}

}
